<template>
  <div class="appendGood">
    <div class="searchGoods">
      <a-input v-model="name" placeholder="请输入名称" style="width: 200px" />
      <a-button type="primary" @click="getList">查询</a-button>
    </div>
    <a-table
      :rowKey="record => record.id"
      :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      :columns="columns"
      :data-source="columnsData"
      :pagination="false"
    >
      <span slot="picture" slot-scope="picture"><img :src="picture" alt=""></span>
    </a-table>
    <Pagination :current="page" :total="total" @pageSize="pageSize" />
  </div>
</template>
<script>
  import Pagination from '@/common/pagination/index'

  export default {
    name:'appendGood',
    components:{Pagination},
    props:{
      good_type:Number
    },
    data(){
      return{
        selectedRowKeys:[],
        columns:[],
        columnsData:[],
        name:'',
        page:1,
        total:0
      }
    },
    created() {
      let config_columns = [
        {
          title: '序号',
          dataIndex: 'good_no',
        },
        {
          title: '',
          dataIndex: 'picture',
          scopedSlots: { customRender: 'picture' }
        },
        {
          title: this.good_type === 1?'商品名称':this.good_type === 2?'服务名称':this.good_type === 3?'套餐名称':'品相名称',
          dataIndex: 'name',
        },
        {
          title: '库存数量',
          dataIndex: 'stock',
        },
        {
          title: '售价',
          dataIndex: 'price',
        },
      ];
      this.columns = config_columns;
      this.getList()
    },
    methods:{
      pageSize(e){
        this.page = e;
        this.getList()
      },
      sendGoods(){
        if(this.selectedRowKeys.length<=0) return this.$message.info("请选择商品");
        let configGood = [];
        this.selectedRowKeys.forEach(item=>{
          this.columnsData.forEach(demo=>{
            if(item === demo.id){
              configGood.push(demo)
            }
          })
        });
        this.$emit('sendData',configGood);
        this.visible = false
      },
      getList(){
        let info = JSON.parse(sessionStorage.getItem('search'));
        info.name = this.name;
        info.page = this.page;
        info.good_type = this.good_type;
        info.size = 10;
        this.Api.cardsGood(info).then(res=>{
          if(res.code !== 200) return;
          this.total = res.data.total;
          this.columnsData = res.data.items
        })
      },
      onSelectChange(selectedRowKeys){
        this.selectedRowKeys = selectedRowKeys;
      }
    }
  }
</script>x
<style lang="less" scoped>
  .appendGood{
    .searchGoods{
      margin-bottom: 10px;
    }
  }
</style>
